Skip to content

fix(codegen): escape reserved identifiers in generated namespaces - #11669

Open
AsadIqbal-Dev wants to merge 1 commit into
dotnet:mainfrom
AsadIqbal-Dev:fix/keyword-generated-namespace
Open

AsadIqbal-Dev wants to merge 1 commit into
dotnet:mainfrom
AsadIqbal-Dev:fix/keyword-generated-namespace

Conversation

@AsadIqbal-Dev

@AsadIqbal-Dev AsadIqbal-Dev commented Nov 2, 2026 •

Copy link
Copy Markdown

Fixes #11640

namespace @event; generated OrleansCodeGen.event, which doesn't compile. GetNamespaceAndNesting returns unescaped names, and four places built the generated namespace from it.

  • GeneratedCodeUtilities.GetGeneratedNamespaceName now escapes each segment. SerializerGenerator, SerializableTypeDescription and ProxyInterfaceDescription call it instead of keeping their own copy. A keyword in an enclosing type's name goes through the same path.
  • GetNamespaceAndNesting is unchanged, because TypeSymbolResolver compares its raw output with model.Namespace.
  • Metadata names can't contain @, so a new GetMetadataNamespaceName removes it where they're built: GeneratedInvokableDescription, InvokableEmissionPlan, ProxyEmissionPlan, MetadataGenerator and SerializerFactoryGenerator. These have to agree. MetadataGenerator matches invokables against names from the proxy output, and a mismatch drops them from the manifest with no compile error. The new test checks for this, and I confirmed it fails when only MetadataGenerator is left unchanged.

EscapeIdentifier also covers contextual keywords such as from and get. A namespace with one of those as a segment already compiled, and now gets an @ in the generated source and a different hint name hash. The generated types and their metadata names don't change.

Tests in ReservedIdentifierNamespaceTests:

  • NamespaceWithReservedKeywordProducesValidGeneratedCode, for @event and Outer.@class.Inner, with an enum, a class, a derived class, a generic class and a grain interface. It also looks up the generated types by metadata name.
  • TypeNestedInReservedKeywordProducesValidGeneratedCode.

The theory fails without the change with the errors from the issue. Orleans.CodeGenerator.Tests passes on net10.0. I only ran that project.

Microsoft Reviewers: Open in CodeFlow

namespace @event generated OrleansCodeGen.event, which doesn't compile.
Escape each segment in one place, and remove the escape again where
metadata names are built, since those are matched against each other and
passed to GetTypeByMetadataName.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Source namespaces containing escaped C# keywords produce invalid generated namespaces

1 participant